<?php


namespace app\admin\controller\Admin;


use app\admin\controller\BaseController;
use app\admin\model\Admin\AdminRoleModel;
use app\admin\model\Admin\AdminRoleRelationModel;
use app\admin\model\Admin\MenuModel;
use app\Utils\Util;
use think\facade\Db;
use think\Request;

class Role extends BaseController
{

    /**
     * 分页查询
     * @param Request $request
     * @return array
     */
    public function getAdminRoleList(Request $request)
    {
        $param = $request->param();
        $where = [];
        $page = $param['page'];
        $limit = $param['limit'];
        $is_show = !empty($param['is_show']) ? $param['is_show'] : "";
        if(empty($page)){
            $page = 1;
        }
        if(empty($limit)){
            $limit = 10;
        }

        if(!empty($is_show)){
            if($is_show == 1){
                array_push($where,['is_show','=',1]);
            }
            if($is_show == 2){
                array_push($where,['is_show','=',0]);
            }
        }

        $list = AdminRoleModel::getAdminRoleListWhere($page,$limit,$where);
        $count = AdminRoleModel::getAdminRoleCountWhere($where);
        return Util::returnPageJson(self::CODE_DATA,$count,$list,"");

    }

    /**
     * 添加
     * @param Request $request
     * @return array
     */
    public function addAdminRole(Request $request)
    {
        $param = $request->param();
        $role_name = $param['role_name'];
        $menu_ids = !empty($param['authids']) ? implode(',',$param['authids']) : "";
        $is_show = !empty($param['is_show']) ? $param['is_show'] : "";

        $data = [
            'role_name'=>$role_name,
            'menu_ids'=>$menu_ids,
            'up_time'=>date("Y-m-d H:i:s",time()),
            'add_time'=>date("Y-m-d H:i:s",time())
        ];

        if(!empty($is_show) && $is_show == "on"){
            $data['is_show'] = 1;
        }else{
            $data['is_show'] = 0;
        }
        $id = AdminRoleModel::addAdminRole($data);
        if($id){
            return Util::returnJson(self::CODE_SUCCESS,$id,"添加成功");
        }else{
            return Util::returnJson(self::CODE_ERROR,null,"添加失败");
        }
    }

    /**
     * 更新
     * @param Request $request
     * @return array
     */
    public function editAdminRole(Request $request)
    {
        $param = $request->param();
        $id = $param['id'];
        $role_name = $param['role_name'];
        $menu_ids = !empty($param['authids']) ? implode(',',$param['authids']) : "";
        $is_show = !empty($param['is_show']) ? $param['is_show'] : "";
        if(empty($id)){
            return Util::returnJson(self::CODE_ERROR,null,"参数错误");
        }

        $save = [
            'role_name'=>$role_name,
            'menu_ids'=>$menu_ids,
            'up_time'=>date("Y-m-d H:i:s",time())
        ];

        if(!empty($is_show)){
            if($is_show == "on"){
                $save['is_show'] = 1;
            }else{
                $save['is_show'] = 0;
            }
        }else{
            $save['is_show'] = 0;
        }
        $flag = AdminRoleModel::updateAdminRoleWhere([['id','=',$id]],$save);
        if($flag){
            return Util::returnJson(self::CODE_SUCCESS,null,"修改成功");
        }else{
            return Util::returnJson(self::CODE_ERROR,null,"修改失败");
        }
    }

    /**
     * 删除
     * @param Request $request
     * @return array
     */
    public function delAdminRole(Request $request)
    {
        $param = $request->param();
        $id = $param['id'];
        //开启事务
        Db::startTrans();
        try{
            //中间逻辑代码
            $flag = AdminRoleModel::delAdminRoleWhere([['id','=',$id]]);
            if($flag){
                //提交事务
                DB::commit();
                return Util::returnJson(self::CODE_SUCCESS,null,"删除成功");
            }else{
                DB::rollBack();
                return Util::returnJson(self::CODE_ERROR,null,"删除失败4");
            }
        }catch (\Exception $e) {
            //接收异常处理并回滚
            //回滚事务
            DB::rollBack();
            return Util::returnJson(self::CODE_ERROR,$e,"删除失败1");
        }
    }

    /**
     * 角色菜单
     * @param Request $request
     * @return array
     */
    public function getAdminRoleTree(Request $request)
    {
        $param = $request->param();
        $id = !empty($param['id']) ? $param['id'] : "";
        $menu_ids = [];
        if(!empty($id)){
            $role = AdminRoleModel::getAdminRoleWhere([['id','=',$id]]);
            if(!empty($role)){
                $menu_ids = explode(',',$role['menu_ids']);
            }
        }
        $menus = MenuModel::getMenuWhereMore([['is_show','=',1]]);
        $tree = Util::setRoleMenuArray($menus,0,[],$menu_ids);
        return Util::returnJson(self::CODE_SUCCESS,$tree);
    }

    /**
     * 获取角色
     * @param Request $request
     * @return array
     */
    public function getAdminRoleAll(Request $request)
    {
        $param = $request->param();
        $role_ids = [];
        $roles = AdminRoleModel::getAdminRoleWhereMore([['is_show','=',1]]);
        if(!empty($param['admin_id'])){
            $list = AdminRoleRelationModel::getAdminRoleRelationWhereMore([['admin_id','=',$param['admin_id']]]);
            if(!empty($list)){
                foreach ($list as $item){
                    array_push($role_ids,$item['role_id']);
                }
            }
        }
        $data = [];
        foreach ($roles as $item){
            $arr['name'] = $item['role_name'];
            $arr['value'] = $item['id'];
            if(!empty($role_ids)){
                if(in_array($item['id'],$role_ids)){
                    $arr['selected'] = true;
                }else{
                    $arr['selected'] = false;
                }
            }else{
                $arr['selected'] = false;
            }
            array_push($data,$arr);
        }
        return Util::returnJson(self::CODE_SUCCESS,$data,"");
    }

}